package goodroommate.ui.info;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import goodroommate.domain.News;
import goodroommate.exception.DatabaseException;
import goodroommate.service.INewsService;
import goodroommate.service.impl.NewsService;

/**
 * Servlet implementation class ShowInfoServlet
 */
@WebServlet("/Info/ShowInfo")
public class ShowInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	INewsService newsService=new NewsService();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowInfoServlet() {
        super();
   
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int newsid=0;
		News news=null;
		try{
			newsid=Integer.parseInt(request.getParameter("newsid"));
		}catch(NumberFormatException e){
			newsid=0;
		}
		
		if(newsid==0){
			request.setAttribute("news", null);
			request.getRequestDispatcher("showinfo.jsp").forward(request, response);
			return;
		}
		try{
			news=newsService.getNewsByID(newsid);
			request.setAttribute("news", news);
			request.getRequestDispatcher("showinfo.jsp").forward(request, response);
			return;
		}catch(DatabaseException e){
			response.sendError(500);
			return ;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}

}
